from django.core.paginator import Paginator
from django.shortcuts import render
from pymongo import MongoClient

# 实例化client，建立连接,我是docker跑的8004端口映射到默认端口27017
client = MongoClient(host='120.27.242.29', port=8004)
collection = client['video']['lan']

def dongman(request,page_id):
    i = 1  # 给视频编号
    # 取出每条数据的title，user_name,以及视频url
    mongo_cursor = collection.find()
    data_list = []
    for data in mongo_cursor:
        data['index'] = i
        data_list.append(data)
        i += 1
    """# 要分页就先拿到要分页的内容传进去，每页展示7条
    paginator = Paginator(data_list, 7)
    print(paginator.num_pages)
    print(paginator.page_range)
    # 没有传页码就默认展示第一页
    if page_id == '':
        # 再用paginator对象的page方法，传入一个页码，返回值values就是第一页的所有数据
        page = paginator.page(1)
        return render(request, 'video.html', {'page': page})
    else:
        page = paginator.page(int(page_id))
        return render(request, 'video.html', {'page': page})"""
    return render(request, 'video.html', {'data_list': data_list})